package com.sbtr.controller;

import com.sbtr.business.jupyterhub.service.impl.JupyterHubServiceImpl;
import com.sbtr.common.Response;
import com.sbtr.open.jupyterhub.dto.JupyterHubDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
@ApiIgnore
@RestController
@RequestMapping("/jupyterhub")
//@Api(tags = "jupyter在线编程模块（废弃，不启动）")
public class JupyterHubController extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private JupyterHubServiceImpl jupyterHubServiceImpl;


	@Value("${jupyter.config.ip}")
	private String jupyterUrl;
//
//	@Value("${security.token4A.username}")
//	private String userName;

	@RequestMapping(value = "findUrl", method = RequestMethod.GET)
	@ApiOperation(value = "根据id查找", notes = "根据id查找", httpMethod = "GET")
	public Response<String> getJupyterUrl() throws IOException {

		return Response.success(jupyterUrl);

	}


	@RequestMapping(value = "find", method = RequestMethod.GET)
	@ApiOperation(value = "根据id查找", notes = "根据id查找", httpMethod = "GET")
	public Response<JupyterHubDto> findByName(String userName) throws IOException {
		// String cookie = req.getHeader("Cookie");
		// try (CloseableHttpClient httpClient = HttpClients.createDefault();) {
		// HttpGet httpGet = new HttpGet(cookiesUrl);
		// httpGet.addHeader("Cookie", cookie);
		// CloseableHttpResponse response = httpClient.execute(httpGet);
		// org.apache.http.HttpEntity entity = response.getEntity();
		// String responseContent = EntityUtils.toString(entity, "UTF-8");
		// JSONObject json = JSONObject.fromObject(responseContent);
		// String username =
		// json.getJSONObject("data").getJSONObject("user").getString("loginName");
		// if (username.length() > 0) {
		// return Response.success(jupyterHubApi.findByName(username));
		// } else {
		// JupyterHubDto jp = new JupyterHubDto();
		// jp.setUserName("用户不存在，请登录");
		// jp.setUserPasswd("用户不存在，请登录");
		// return Response.success(jp);
		// }
		// }

		return Response.success(jupyterHubServiceImpl.findByName(userName));
	}





}
